package com.tarena.lbs.basic.web.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.base.protocol.pager.BasePageQuery;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.basic.web.service.StoreService;
import com.tarena.lbs.common.basic.converter.StoreConverter;
import com.tarena.lbs.pojo.basic.bo.StoreBO;
import com.tarena.lbs.pojo.basic.param.StoreParam;
import com.tarena.lbs.pojo.basic.query.AreaStoreQuery;
import com.tarena.lbs.pojo.basic.query.StoreQuery;
import com.tarena.lbs.pojo.basic.vo.StoreVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/basic/")
public class StoreController {
    @Autowired
    private StoreService storeService;

    //后台店铺分页查询
    @PostMapping("store/getAll")
    public Result<PageResult<StoreVO>> storeList(@RequestBody StoreQuery query)throws BusinessException {
        //业务层返回 basePageList total objects
        log.debug("C1-商家列表查询:{}",query);
        BasePageList<StoreBO> boPages= storeService.storeList(query);
        PageResult<StoreVO> pageVOS = assemblePageVOS(boPages, query);
        log.debug("C1-返回商家数据:{}",pageVOS.getObjects());
        return new Result<>(pageVOS);
    }

    //新增门店 店铺数据
    @PostMapping("store/add")
    public Result<Void> saveStore(@RequestBody StoreParam storeParam) throws BusinessException{
        //TODO 参考规范约定 业务层处理BO数据
        //参数中没有es 属性location的
        log.debug("C2-新增门店:{}",storeParam);
        storeService.saveStore(storeParam);
        return Result.success();
    }
    @PostMapping("store/getStoreByCity")
    public Result<PageResult<StoreVO>> getCitiesStores(@RequestBody AreaStoreQuery query) throws BusinessException{
        BasePageList<StoreBO> storeBOS=storeService.getCitiesStores(query);
        PageResult<StoreVO> storeVOPageResult = assemblePageVOS(storeBOS, query);
        return new Result<>(storeVOPageResult);
    }

    //数据处理
    public PageResult<StoreVO> assemblePageVOS(BasePageList<StoreBO> bos, BasePageQuery query) {
        if (bos == null || bos.getTotal() == 0) {
            return null;
        }
        PageResult<StoreVO> result = new PageResult<>();
        List<StoreVO> vos = new ArrayList<>();
        for (StoreBO temp : bos.getList()) {
            StoreVO vo = new StoreVO();
            BeanUtils.copyProperties(temp,vo);
            vos.add(vo);
        }
        result.setTotal(bos.getTotal());
        result.setObjects(vos);
        result.setPageNo(query.getPageNo());
        result.setPageSize(query.getPageSize());
        return result;
    }
}
